package jnpf.database.sql;

import jnpf.database.model.DbFieldMod;
import jnpf.database.model.DbTableFieldModel;
import jnpf.database.source.DbBase;
import jnpf.database.sql.append.create.CreateSql;
import jnpf.database.sql.append.create.CreateSqlDTO;
import jnpf.database.util.DataSourceUtil;

import java.util.List;

/**
 * SQL语句模板基类
 *
 * @author JNPF开发平台组
 * @version V3.2.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/10/6
 */
public abstract class SqlBase {

    /**
     * 数据基类
     */
    protected DbBase dbBase;

    protected String fieldListSql;

    protected String tableListSql;

    /**
     * 构造初始化
     *
     * @param dbBase
     */
    public SqlBase(DbBase dbBase) {
        this.dbBase = dbBase;
        setSqlBase();
    }

    /**
     * 初始设置参数
     */
    protected abstract void setSqlBase();

    /**
     * 查询字段SQL
     */
    public String getFieldListSql(String table, DataSourceUtil dbSourceOrDbLink) {
        return this.dbBase.getReplaceSql(this.fieldListSql, table, dbSourceOrDbLink);
    }

    ;

    /**
     * 查询表SQL
     */
    public String getTableListSql(DataSourceUtil dbSourceOrDbLink) {
        return this.dbBase.getReplaceSql(this.tableListSql, "", dbSourceOrDbLink);
    }

    /**
     * 获取表数据SQL
     *
     * @param table
     * @return
     */
    public String getDataSql(String table) {
        return "SELECT * FROM " + table;
    }

    /**
     * 删除表SQL
     *
     * @param table
     * @return
     */
    public String getDeleteSql(String table) {
        return "DROP TABLE IF EXISTS " + table + ";";
    }

    /**
     * 重命名SQL
     *
     * @param oldTable
     * @param newTable
     * @return
     */
    public String getRenameSql(String oldTable, String newTable) {
        return "ALTER TABLE " + oldTable + " RENAME TO " + newTable + ";";
    }

    /**
     * 添加表SQL
     *
     * @param table                 阿斯蒂芬
     * @param tableComment          阿达地方
     * @param dbTableFieldModelList 大沙发斯蒂芬
     * @return
     */
    public String getCreateSql(String table, String tableComment, List<DbTableFieldModel> dbTableFieldModelList) throws Exception {
        return CreateSql.getCreTabSql(new CreateSqlDTO(this.dbBase, table, tableComment, dbTableFieldModelList));
    }


    /**
     * 批量添加数据
     *
     * @param dataList
     * @param table
     * @return
     */
    // TODO 其余几个数据还没有添加方法
    public String batchInsertSql(List<List<DbFieldMod>> dataList, String table) {
        return "";
    }

    ;

    public abstract String[] getPageSql(String sql, String sortType, Integer currentPage, Integer pageSize);

}
